import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {PaySucceedPage} from "../pay-succeed/pay-succeed";
import {ROUTER} from "../../service/router.service";
import {APARTMENTAPISERVICE} from "../../api/apartment-api.service";
import {METER} from "../../api/meter.api";
import {PageParam} from "../../api/api.service";
import {LocalStorage} from "../../utils/localstorage.utils";
declare var waveLoading:any;

/**
 * Generated class for the LifePaymentPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-life-payment-page',
  templateUrl: 'life-payment-page.html',
})
export class LifePaymentPage {
  pageParam:PageParam;
  hasMore:boolean = true;
  contractId:string;
  houseList:any=[];
  billList = [{
    "id": 1,
    "month": "1月",
    "week": "星期日",
    "noteTime": "2017-01-22 10:51:39",
    "type": 1,
    "typeName": "水费",
    "content": "20度",
    "money": 20
  },{
    "id": 2,
    "month": "1月",
    "week": "星期日",
    "noteTime": "2017-01-22 10:51:39",
    "type": 2,
    "typeName": "电费",
    "content": "30顿",
    "money": 20
  }];


  constructor(public navCtrl:NavController,
              public navParams:NavParams,
              public APARTMENTAPISERVICE:APARTMENTAPISERVICE,
              public METER:METER,
              public ls:LocalStorage,
              private ROUTER:ROUTER) {
  }

  ionViewDidLoad() {
    this.loadMyHouse();

    console.log('ionViewDidLoad LifePaymentPage');
    waveLoading.load1.init({
      target: '#ebalance',
      color: '#F5B88D',
      showText: true
    });
    waveLoading.load1.draw();
    waveLoading.load2.init({
      target: '#wbalance',
      color: '#C3DFFE',
      showText: true
    });
    waveLoading.load2.draw();
    waveLoading.load1.setProgress(0)
    waveLoading.load2.setProgress(0)
  }
  loadMyHouse(){
    this.APARTMENTAPISERVICE.getMyRentingHouse({},(res:any)=>{
      this.houseList = res;
      if(this.houseList.length >0){
        this.contractId = this.houseList[0].contractId

        this.doRefresh();
      }
    })
  }

  gotoPay() {
    this.ls.set('lcContractId',this.contractId)
    this.ROUTER.go(ROUTER.LIFERECHARGE);
  }

  doRefresh(refresher?) {
    this.METER.getBalance(this.contractId,v=>{
      waveLoading.load1.setProgress(v.powerDegree)
      waveLoading.load2.setProgress(v.waterDegree)
    })
    this.pageParam = new PageParam()
    this.METER.getList(v => {
      this.billList = v.content
      console.log(this.billList);
      this.hasMore = !v.last;
      if (refresher) {
        refresher.complete();
      }
    },this.contractId, this.pageParam);
    setTimeout(v=>{
      if (refresher) {
        refresher.complete();
      }
    },3000)
  }

  get canLoadMore():boolean {
    return this.hasMore;
  }

  doInfinite(infiniteScroll) {
    ++this.pageParam.page;
    this.METER.getList(v => {
      this.hasMore = !v.last;
      this.billList = this.billList.concat(v.content);
      infiniteScroll.complete();
    },this.contractId, this.pageParam);
    setTimeout(v=>{
      if (infiniteScroll) {
        infiniteScroll.complete();
      }
    },3000)
  }


}
